package lb.io.utils;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class Strings {

    private final static String letterWithDigit = "0123456789abcdefghijklmnopqrstuvwxyz";

    private final static String lowerLetter = "abcdefghijklmnopqrstuvwxyz";

    /**
     * 统计字符串中某个字符出项的次数
     *
     * @param s
     * @param substr
     * @return
     */
    public static int Count(String s, String substr) {
        if (substr.equals("")) {
            return 0;
        }
        int n = 0;
        int i;
        while (true) {
            i = s.indexOf(substr);
            if (i == -1) {
                return n;
            }
            n++;
            s = s.substring(i + substr.length());
        }
    }

    /**
     * 清除字符串 s 中 substr 开头的字符串，如果字符串 s 以 substr 为开头的话
     *
     * @param s
     * @param substr
     * @return
     */
    public static String TrimPrefix(String s, String substr) {
        if (s.startsWith(substr)) {
            return s.substring(s.indexOf(substr) + substr.length());
        }
        return s;
    }

    /**
     * 清除字符串 s 尾部的 substr 字符串，如果字符串 s 以 substr 为结尾的话
     *
     * @param s
     * @param substr
     * @return
     */
    public static String TrimSuffix(String s, String substr) {
        if (s.endsWith(substr)) {
            return s.substring(0, s.lastIndexOf(substr));
        }
        return s;
    }

    /**
     * 返回系统文件分隔符
     *
     * @return
     */
    public static String FileSeparator() {
        return System.getProperties().getProperty("file.separator");
    }

    /**
     * 返回 resource\
     *
     * @return
     */
    public static String LBRoot() {
        return "resource" + FileSeparator();
    }

    /**
     * 返回一个随机的 uuid
     *
     * @return
     */
    public static String UUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 字符串转化为 uuid
     *
     * @param name
     * @return
     */
    public static String UUIDBuild(String name) {
        return UUID.nameUUIDFromBytes(name.getBytes()).toString();
    }

    /**
     * 返回一个随机数
     *
     * @param n
     * @return
     */
    private synchronized static int randIntn(int n) {
        final long interval = 1;
        long nono = System.nanoTime();
        long end = 0;
        // 模拟睡眠 1ns
        do {
            end = System.nanoTime();
        } while (nono + interval >= end);
        return new Random(end).nextInt(n);
    }

    /**
     * 返回一串只包含数字和小写字母的字符串
     *
     * @param n
     * @return
     */
    public static String RandomName(int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int rand = randIntn(letterWithDigit.length());
            sb.append(letterWithDigit, rand, rand + 1);
        }
        return sb.toString();
    }

    /**
     * 返回一串小写字母组成的字符串
     *
     * @param n
     * @return
     */
    public static String RandomChar(int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int rand = randIntn(lowerLetter.length());
            sb.append(lowerLetter, rand, rand + 1);
        }
        return sb.toString();
    }

    public static String Join(List<String> s, char seq) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.size(); i++) {
            String item = s.get(i);
            if (item.trim().equals("")) {
                continue;
            }
            if (i == s.size() - 1) {
                sb.append(item);
            } else {
                sb.append(item);
                sb.append(seq);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        List<String> s = new LinkedList<>();
        s.add("hello");
        s.add(" ");
        s.add("world");
        System.out.println(Join(s, ','));
    }
}
